﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using OrganizedConfusion.Game_Objects;

namespace OrganizedConfusion
{
    public class ObjectManager
    {
        private static ObjectManager instance;
        private List<BaseObject> ObjList;
        List<Player> PlayerList = new List<Player>();

        private ObjectManager()
        {
            ObjList = new List<BaseObject>();
        }

        /// <summary>
        /// Returns a new instance of the class if hasn't already made one
        /// </summary>
        /// <returns></returns>
        public static ObjectManager GetInstance()
        {
            lock (typeof(ObjectManager))
            {
                if (instance == null)
                    instance = new ObjectManager();
                return instance;
            }
        }

        /// <summary>
        /// Adds an object to the object list
        /// </summary>
        /// <param name="obj"></param>
        public void AddObject(BaseObject obj)
        {
            if (obj != null)
            {
                for (int i = 0; i < ObjList.Count; i++)
                {
                    if(ObjList[i].Equals(obj))
                        return;
                }
                ObjList.Add(obj);
                if (obj.Type == (int)BaseObject.OBJ_TYPE.OBJ_PLAYER)
                    PlayerList.Add((Player)obj);
            }
        }

        /// <summary>
        /// Removes an object from the object list
        /// </summary>
        /// <param name="obj"></param>
        public void RemoveObject(BaseObject obj)
        {
            if (obj != null)
            {
                for (int i = ObjList.Count; i > -1; i--)
                {
                    if (ObjList[i].Equals(obj))
                    {
                        ObjList.RemoveAt(i);
                        i--;
                    }
                }
            }
        }

        /// <summary>
        /// Returns a list of all current players
        /// </summary>
        /// <returns></returns>
        public List<Player> GetPlayers()
        {
            //foreach (Player p in ObjList)
            //    PlayerList.Add(p);

            return PlayerList;
        }

        /// <summary>
        /// Makes a seperate list that renders only objects that are on screen.
        /// </summary>
        public void RenderOnScreenObjects(SpriteBatch sb, GameTime gametime, Viewport cam)
        {
            //sb.Begin(SpriteSortMode.Deferred, null, null, null, null, null, CamManager.GetCamera(i).GetViewMatrix(Vector2.One));

            sb.End();
        }

        /// <summary>
        /// Returns a list of all onscreen objects.
        /// </summary>
        /// <param name="cam"></param>
        /// <returns></returns>
        public List<BaseObject> MakeDrawingList(CameraClass cam)
        {
            List<BaseObject> RenderList = new List<BaseObject>();
            for (int i = 0; i < ObjList.Count; i++)
            {
                if (ObjList[i].Position.X < (cam.Position.X + (cam.Port.Width * 2)) && (ObjList[i].Position.X + ObjList[i].ImageDimensions.X) > cam.Position.X - (ObjList[i].ImageDimensions.X * 2))
                {
                    if (ObjList[i].Position.Y < (cam.Position.Y + (cam.Port.Height * 2)) && (ObjList[i].Position.Y + ObjList[i].ImageDimensions.Y) > cam.Position.Y - (ObjList[i].ImageDimensions.Y * 2))
                    {
                        if (ObjList[i].Type == (int)BaseObject.OBJ_TYPE.OBJ_PLAYER)
                            continue;
                        RenderList.Add(ObjList[i]);
                    }
                }
            }
            return RenderList;
        }

        /// <summary>
        /// Does Collision checks against the player
        /// </summary>
        /// <param name="list"></param>
        /// <param name="user"></param>
        public void CheckCollision(List<BaseObject> list, Player user)
        {
            for (int j = 0; j < list.Count; j++)
                list[j].CheckCollision(user);
        }

        public void RenderObjects(List<BaseObject> objs, SpriteBatch sb)
        {
            foreach (BaseObject obj in objs)
                obj.Render(sb);
        }
    }
}
